package control;

import net.Client;
import util.Parser;
import util.Protocol;

/**
 * 客户端操作类，负责处理图书管理系统的各种业务操作
 * 通过网络通信与服务器交互，实现图书的增删改查功能
 */
public class Operator {

	/**
	 * 添加图书
	 * @param bookname 图书名称
	 * @param author 作者
	 * @param price 价格
	 * @return 添加是否成功
	 */
	public boolean addBook(String bookname, String author, float price) {
		Protocol protocol = new Protocol();
		String msg = protocol.getAddBookMsg(bookname, author, price); // 打包查询语句
		Client.write(msg); // 发送信息给服务器，服务器处理请求
		String returnStr = Client.read();
		System.out.println(returnStr);
		Parser parser = new Parser();
		boolean isSuccess = parser.getIsSuccess(returnStr);
		return isSuccess;
	}

	/**
	 * 根据书名删除图书
	 * @param bookname 要删除的图书名称
	 * @return 删除是否成功
	 */
	public boolean delBookByBookname(String bookname) {
		Protocol protocol = new Protocol();
		String msg = protocol.getDelByBooknameMsg(bookname);
		Client.write(msg);
		String returnStr = Client.read();
		Parser parser = new Parser();
		boolean isSuccess = parser.getIsSuccess(returnStr);
		return isSuccess;
	}

	/**
	 * 根据作者删除图书
	 * @param author 要删除的图书作者
	 * @return 删除是否成功
	 */
	public boolean delBookByAuthor(String author) {
		Protocol protocol = new Protocol();
		String msg = protocol.getDelByAuthorMsg(author);
		Client.write(msg);
		String returnStr = Client.read();
		Parser parser = new Parser();
		boolean isSuccess = parser.getIsSuccess(returnStr);
		return isSuccess;
	}

	/**
	 * 根据价格范围删除图书
	 * @param lowBound 价格下限
	 * @param upBound 价格上限
	 * @return 删除是否成功
	 */
	public boolean delBookByPrice(float lowBound, float upBound) {
		Protocol protocol = new Protocol();
		String msg = protocol.getDelByPriceMsg(lowBound, upBound);
		Client.write(msg);
		String returnStr = Client.read();
		Parser parser = new Parser();
		boolean isSuccess = parser.getIsSuccess(returnStr);
		return isSuccess;
	}

	/**
	 * 根据书名查询图书
	 * @param bookname 要查询的图书名称
	 * @return 查询到的图书信息二维数组
	 */
	public String[][] fndByBookname(String bookname) {
		Protocol protocol = new Protocol();
		String msg = protocol.getFndByBooknameMsg(bookname);
		Client.write(msg);
		String returnStr = Client.read();
		Parser parser = new Parser();
		String[][] booklist = parser.fndBookParser(returnStr);
		return booklist;
	}

	/**
	 * 根据作者查询图书
	 * @param author 要查询的图书作者
	 * @return 查询到的图书信息二维数组
	 */
	public String[][] fndByAuthor(String author) {
		Protocol protocol = new Protocol();
		String msg = protocol.getFndByAuthorMsg(author);
		Client.write(msg);
		String returnStr = Client.read();
		Parser parser = new Parser();
		String[][] booklist = parser.fndBookParser(returnStr);
		return booklist;
	}

	/**
	 * 根据价格范围查询图书
	 * @param lowbound 价格下限
	 * @param upbound 价格上限
	 * @return 查询到的图书信息二维数组
	 */
	public String[][] fndByPrice(float lowbound, float upbound) {
		Protocol protocol = new Protocol();
		String msg = protocol.getFndByPriceMsg(lowbound, upbound);
		Client.write(msg);
		String returnStr = Client.read();
		Parser parser = new Parser();
		String[][] booklist = parser.fndBookParser(returnStr);
		return booklist;
	}

	/**
	 * 修改图书信息
	 * @param bookname 原图书名称
	 * @param altbookname 新图书名称
	 * @param altauthor 新作者
	 * @param altprice 新价格
	 * @return 修改是否成功
	 */
	public boolean altBook(String bookname, String altbookname, String altauthor, float altprice) {
		Protocol protocol = new Protocol();
		String msg = protocol.getAltBookMsg(bookname, altbookname, altauthor, altprice);
		Client.write(msg);
		String returnStr = Client.read();
		Parser parser = new Parser();
		boolean isSuccess = parser.getIsSuccess(returnStr);
		return isSuccess;
	}

	/**
	 * 打印所有图书信息
	 * @return 所有图书信息的二维数组
	 */
	public String[][] prtAllBook() {
		Protocol protocol = new Protocol();
		String msg = protocol.getPrtAllBookMsg();
		Client.write(msg);
		System.out.println("msg:" + msg);
		String returnStr = Client.read();
		System.out.println("returnStr:" + returnStr);
		Parser parser = new Parser();
		String[][] booklist = parser.fndBookParser(returnStr);
		return booklist;
	}
}
